package com.obrempon.credit.market.ui.dialog

import android.content.Context
import android.text.SpannableString
import android.text.style.StrikethroughSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonDialog
import com.obrempon.credit.market.databinding.ObremponLimitIncreasedDialogBinding
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.utils.ObremponImageUtil
import com.obrempon.credit.market.weight.viewpager.entity.ProductBean
import org.json.JSONObject
import java.math.BigDecimal


/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：产品额度提升dialog
 */
class ObremponLimitIncreasedDialog: CommonDialog {
    private lateinit var binding: ObremponLimitIncreasedDialogBinding
    private var mCallback: IDialogCallback? = null
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, eventRemark: String, callback: IDialogCallback?) : super(context) {
        this.eventRemark = eventRemark
        mCallback = callback
    }

    override fun initObremponWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window!!.setGravity(Gravity.CENTER)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeObremponRootView(): View {
        binding = ObremponLimitIncreasedDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initObremponView() {

    }

    override fun initObremponListener() {
        setOnClickListener(binding.buttonApplyNow) //申请
        setOnClickListener(binding.imageLimitClose) //关闭
    }

    override fun takeObremponData() {
    }

    override fun onObremponViewClick(v: View?) {
        dismiss()
        if (v!!.id == R.id.buttonApplyNow){
            if(mCallback != null){
                mCallback!!.onClick(v)
            }
        }
    }

    /**
     * 设置数据
     */
    fun showObremponLimitIncreasedData(data: JSONObject){
        ObremponImageUtil.instance.loadImageUrl(context,binding.imageProductLogo,data.optString("pLogo"),R.mipmap.obrempon_logo)
        binding.textProductName.text = data.optString("pName")
        binding.textImproveAmount.text = BigDecimal(data.optString("amount")).stripTrailingZeros().toPlainString()
        val text = BigDecimal(data.optString("oldAmount")).stripTrailingZeros().toPlainString()
        val content = SpannableString(text)
        content.setSpan(StrikethroughSpan(), 0, text.length, 0)
        binding.textOldAmount.text = content
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_LIMIT_INCREASED_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_LIMIT_INCREASED_DISMISS,eventRemark)
    }


    /**
     *
     * 接口描述：product额度相较比于上一次是否有变化
     * 接口名：/v1/amountIsUp
     * 参数：公共参数
     * 注意：贷超包product和token参数，传所需要查询的产品对应的值
     * 返回值：
     * openFlag，值"YES"或"NO"
     * 当openFlag=“YES”时，会多两个返回值：amount、oldAmount
     *
     */

}